request.js:64


POST http://localhost:8080/api/seller/products 401 (Unauthorized)<!-- src/views/SearchResults.vue -->
<template>
  <div class="search-results">
    <Header />
    <div class="container">
      <div class="search-header-row">
        <el-button type="text" @click="goBack" class="back-btn">
          <el-icon style="vertical-align: middle;"><ArrowLeft /></el-icon>
          返回
        </el-button>
        <h1 class="search-title">搜索结果：{{ keyword }}</h1>
      </div>
      <el-row :gutter="32" justify="start">
        <el-col
          v-for="item in products"
          :key="item.productId"
          :xs="24"
          :sm="12"
          :md="6"
          :lg="6"
          :xl="6"
        >
          <el-card class="product-card" shadow="hover" @click="goDetail(item.productId)" style="cursor:pointer;">
            <img :src="item.mainImage" class="product-img" />
            <div class="product-info">
              <div class="product-name">{{ item.name }}</div>
              <div class="product-price">￥{{ item.price }}</div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <el-pagination
        v-if="total > pageSize"
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="page"
        @current-change="handlePageChange"
        style="margin-top: 30px; text-align: center;"
      />
      <div v-if="!loading && products.length === 0" class="placeholder-text">没有找到相关商品</div>
    </div>
  </div>
</template>

<script setup>
import Header from '@/components/Header.vue';
import { useRoute, useRouter } from 'vue-router';
import { ref, watch, onMounted } from 'vue';
import request from '@/utils/request';
import { ArrowLeft } from '@element-plus/icons-vue';

const route = useRoute();
const router = useRouter();
const keyword = ref(route.query.keyword || '');
const products = ref([]);
const total = ref(0);
const page = ref(1);
const pageSize = ref(12);
const loading = ref(false);

const goBack = () => {
  router.back();
};

const fetchProducts = async () => {
  loading.value = true;
  try {
    const res = await request.get('/api/products', {
      params: {
        keyword: keyword.value,
        page: page.value,
        pageSize: pageSize.value,
        status: 1
      }
    });
    products.value = res.list || [];
    total.value = res.total || 0;
  } finally {
    loading.value = false;
  }
};

const handlePageChange = (val) => {
  page.value = val;
  fetchProducts();
};

const goDetail = (id) => {
  router.push(`/products/${id}`);
};

watch(() => route.query.keyword, (newVal) => {
  keyword.value = newVal;
  page.value = 1;
  fetchProducts();
});

onMounted(fetchProducts);
</script>

<style scoped>
.search-results {
  min-height: 100vh;
  background: #f8fafc;
}
.container {
  max-width: 1200px;
  margin: 40px auto;
  padding: 0 20px;
  width: 100%;
}
.search-header-row {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  position: relative;
}
.back-btn {
  margin-right: 16px;
  font-size: 18px;
  position: absolute;
  left: 0;
}
.search-title {
  flex: 1;
  text-align: center;
  margin: 0;
  font-size: 32px;
  color: #1a365d;
  font-weight: bold;
  letter-spacing: 2px;
}
.el-row {
  margin-left: -16px !important;
  margin-right: -16px !important;
}
.el-col {
  padding-left: 16px !important;
  padding-right: 16px !important;
}
.product-card {
  border-radius: 16px;
  box-shadow: 0 2px 12px rgba(67,97,238,0.08);
  transition: box-shadow 0.2s;
  margin-bottom: 32px;
  min-height: 320px;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  padding: 18px 0 10px 0;
}
.product-card:hover {
  box-shadow: 0 4px 24px rgba(67,97,238,0.18);
  border-color: #409eff;
}
.product-img {
  width: 180px;
  height: 180px;
  object-fit: contain;
  margin: 24px auto 12px auto;
  display: block;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 1px 6px rgba(0,0,0,0.04);
}
.product-info {
  text-align: center;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  width: 100%;
}
.product-name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 8px;
  color: #222d32;
  word-break: break-all;
}
.product-price {
  color: #f56c6c;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 16px;
}
.placeholder-text {
  font-size: 20px;
  color: #718096;
  text-align: center;
  padding: 50px 0;
}
</style>